package jehc.cloud.gen.web;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

import afu.org.checkerframework.checker.oigj.qual.O;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jehc.cloud.common.annotation.NeedLoginUnAuth;
import jehc.cloud.common.base.BasePage;
import jehc.cloud.common.base.BaseResult;
import jehc.cloud.common.base.BaseSearch;
import jehc.cloud.common.util.FastJsonUtils;
import jehc.cloud.common.util.JsonUtil;
import jehc.cloud.common.util.StringUtil;
import jehc.cloud.common.util.dbtools.DBMSMetaUtil;
import jehc.cloud.common.util.dbtools.DbInfo;
import jehc.cloud.common.util.dbtools.MapUtil;
import jehc.cloud.gen.base.BaseAction;
import jehc.cloud.gen.model.*;
import jehc.cloud.gen.service.XtDbinfoService;
import jehc.cloud.gen.service.XtGeneratorForbidtableService;
import jehc.cloud.gen.service.XtGeneratorService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageInfo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.web.servlet.ModelAndView;
/**
 * @Desc 生成数据层代码(包括接口Dao,实现类DaoImpl,Mybatis等代码)
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Controller
@RequestMapping("/xtGenerator")
@Api(value = "代码生成器API",description = "代码生成器API")
public class XtGeneratorController extends BaseAction {
	@Autowired
	private XtGeneratorService xtGeneratorService;
	@Autowired
	private XtGeneratorForbidtableService xtGeneratorForbidtableService;

	@Autowired
	private XtDbinfoService xtDbinfoService;

	/**
	 * 列表页面
	 * @param model
	 * @return
	 */
	@ApiOperation(value="列表页面", notes="列表页面")
	@RequestMapping(value="/pageList")
	public ModelAndView pageList( Model model) {
//		DbInfo dbInfo = DBMSMetaUtil.excuteDB(null);
//		model.addAttribute("dbInfo", dbInfo);
		return new ModelAndView("xt-generator/xt-generator-list");
	}

//	/**
//	 * 新增页面
//	 * @param model
//	 * @return
//	 */
//	@ApiOperation(value="新增页面", notes="新增页面")
//	@RequestMapping(value="/pageAdd")
//	public ModelAndView pagAdd( Model model) {
//		DbInfo dbInfo = DBMSMetaUtil.excuteDB(null);
//		model.addAttribute("dbInfo", dbInfo);
//		return new ModelAndView("sys-generator/sys-generator-add");
//	}
//
//	/**
//	 * 修改页面
//	 * @param model
//	 * @return
//	 */
//	@ApiOperation(value="修改页面", notes="修改页面")
//	@RequestMapping(value="/pageUpdate")
//	public ModelAndView pageUpdate( Model model) {
//		DbInfo dbInfo = DBMSMetaUtil.excuteDB(null);
//		model.addAttribute("dbInfo", dbInfo);
//		return new ModelAndView("sys-generator/sys-generator-update");
//	}
	
	/**
	 * 分页查询
	 * @param baseSearch
	 */
	@NeedLoginUnAuth
	@PostMapping(value="/list")
	@ApiOperation(value="分页查询", notes="分页查询")
	@ResponseBody
	public BasePage getXtGeneratorListByCondition(BaseSearch baseSearch){
		Map<String, Object> condition = baseSearch.convert();
		commonHPager(baseSearch);
		List<XtGenerator> xtGeneratorList = xtGeneratorService.getXtGeneratorListByCondition(condition);
		PageInfo<XtGenerator> page = new PageInfo<XtGenerator>(xtGeneratorList);
		return outPageStr(page,baseSearch);
	}

	/**
	 * 查询指定库中指定表中所有字段
	 * @param xt_dbinfo_id
	 * @param xt_generator_tbname
	 * @return
	 */
	@NeedLoginUnAuth
	@PostMapping(value="/table/column/list")
	@ApiOperation(value="获取表中所有字段用于显示字段列表", notes="获取表中所有字段用于显示字段列表")
	@ResponseBody
	public BaseResult getXtGeneratorTableColumnListByCondition(String xt_dbinfo_id,String xt_generator_tbname){
		List<XtGeneratorTableColumn> xtGeneratorTableColumnList = new ArrayList<XtGeneratorTableColumn>();
		//支持多数据库如Mysql oracle  sqlserver sybase db2
		XtDbinfo xtDbinfo = xtDbinfoService.getXtDbinfoById(xt_dbinfo_id);

		DbInfo info = convert(xtDbinfo);
		info.setTableName(xt_generator_tbname);
		DbInfo dbInfo = DBMSMetaUtil.excuteDB(info);
		String primaryKey="";
		List<Map<String, Object>> columnsPrimary = MapUtil.convertKeyList2LowerCase(dbInfo.getColumnsPrimary());
		if(!columnsPrimary.isEmpty() && columnsPrimary.size()>0){
			Map<String, Object> map = columnsPrimary.get(0);
			primaryKey = map.get("column_name").toString();
		}
		List<Map<String, Object>> columns = dbInfo.getColumns();
		if(!columns.isEmpty() && columns.size()>0){
			for(int i = 0; i < columns.size(); i++){
				Map<String, Object> map = columns.get(i);
				XtGeneratorTableColumn xt_Generator_Table_Column = new XtGeneratorTableColumn();
				if(primaryKey.equals(""+map.get("COLUMN_NAME"))){
					xt_Generator_Table_Column.setCOLUMN_KEY("PRI");
				}
				xt_Generator_Table_Column.setCOLUMN_NAME(""+map.get("COLUMN_NAME"));
				xt_Generator_Table_Column.setCOLUMN_COMMENT(""+map.get("REMARKS"));
				xt_Generator_Table_Column.setDATA_TYPE(""+map.get("TYPE_NAME"));
				xt_Generator_Table_Column.setIS_NULLABLE(""+map.get("IS_NULLABLE"));
				xt_Generator_Table_Column.setCHARACTER_MAXIMUM_LENGTH(""+map.get("COLUMN_SIZE"));
				xt_Generator_Table_Column.setColumn_label_position("居左");
				xt_Generator_Table_Column.setColumn_label_anchor("100");
				xt_Generator_Table_Column.setIsHidden("否");
				xtGeneratorTableColumnList.add(xt_Generator_Table_Column);
			}
		}
		return outItemsStr(xtGeneratorTableColumnList);
	}

	/**
	 * 查询指定库中指定表中所有字段
	 * @param xt_dbinfo_id
	 * @param xt_generator_tbname
	 * @return
	 */
	@NeedLoginUnAuth
	@GetMapping(value="/table/column/pk/list")
	@ApiOperation(value="获取主键", notes="获取主键")
	@ResponseBody
	public BaseResult getXtGeneratorTableColumnPkList(String xt_dbinfo_id,String xt_generator_tbname){
		//支持多数据库如Mysql oracle  sqlserver sybase db2
		XtDbinfo xtDbinfo = xtDbinfoService.getXtDbinfoById(xt_dbinfo_id);

		DbInfo info = convert(xtDbinfo);
		info.setTableName(xt_generator_tbname);
		DbInfo dbInfo = DBMSMetaUtil.excuteDB(info);
		List<Map<String, Object>> columnsPrimary = MapUtil.convertKeyList2LowerCase(dbInfo.getColumnsPrimary());
		List<XtGeneratorTableColumn> xtGeneratorTableColumnList = new ArrayList<>();
		for(int i = 0; i < columnsPrimary.size(); i++){
			XtGeneratorTableColumn xtGeneratorTableColumn = new XtGeneratorTableColumn();
			Map<String, Object> map = columnsPrimary.get(i);
			if(null != map.get("remarks") && !"".equals(map.get("remarks"))){
				xtGeneratorTableColumn.setCOLUMN_COMMENT(""+map.get("remarks"));
			}else{
				xtGeneratorTableColumn.setCOLUMN_COMMENT(""+map.get("column_name"));
			}
			xtGeneratorTableColumn.setCOLUMN_NAME(""+map.get("column_name"));
			xtGeneratorTableColumnList.add(xtGeneratorTableColumn);
		}
		return outItemsStr(xtGeneratorTableColumnList);
	}

	/**
	 * 生成代码
	 * @param xtGeneratorEntity
	 * @return
	 */
	@PostMapping(value="/add")
	@ApiOperation(value="生成代码", notes="生成代码")
	@ResponseBody
	public BaseResult addXtGenerator(@RequestBody XtGeneratorEntity xtGeneratorEntity){
		XtGenerator xtGenerator = xtGeneratorEntity;
		xtGenerator.setXt_generator_id(toUUID());
		xtGenerator.setXt_Generator_Grid_ColumnList(commonGeneratorGridListJSONList(xtGeneratorEntity.getXtGeneratorGridListJSON()));
		xtGenerator.setXt_generator_search_filedList(commonGeneratorSearchFileJSONList(xtGeneratorEntity.getXtGeneratorSearchFileJSON()));
		xtGenerator.setXt_Generator_Table_Column_FormList(commonGeneratorFormJSONList(xtGeneratorEntity.getXtGeneratorTableColumnFormJson()));
		xtGenerator.setXt_Generator_TableMany_To_OneList(commonXtGeneratorOneToManyFormListJSON(xtGeneratorEntity.getXtGeneratorOneToManyFormListJSON()));
		int i = xtGeneratorService.addXtGenerator(xtGenerator);
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 查询所有表
	 * @param xt_dbinfo_id
	 * @return
	 */
	@NeedLoginUnAuth
	@PostMapping(value="/table/list")
	@ApiOperation(value="查询所有表", notes="查询所有表")
	@ResponseBody
	public BaseResult getXtGeneratorTableList(String xt_dbinfo_id){
		//支持多数据库如Mysql oracle  sqlserver sybase db2
		List<XtGeneratorTable> xtGeneratorTableList = new ArrayList<XtGeneratorTable>();
		XtDbinfo xtDbinfo = xtDbinfoService.getXtDbinfoById(xt_dbinfo_id);
		DbInfo dbInfo = DBMSMetaUtil.excuteDB(convert(xtDbinfo));
		List<Map<String, Object>> tables = MapUtil.convertKeyList2LowerCase(dbInfo.getTables());
		for(int i = 0; i < tables.size(); i++){
			Map<String, Object> map = tables.get(i);
			XtGeneratorTable xt_Generator_Table = new XtGeneratorTable();
			if(null != map.get("remarks") && !"".equals(map.get("remarks"))){
				xt_Generator_Table.setTABLE_COMMENT(""+map.get("remarks"));
			}else{
				xt_Generator_Table.setTABLE_COMMENT(""+map.get("table_name"));
			}
			xt_Generator_Table.setTABLE_NAME(""+map.get("table_name"));
			xtGeneratorTableList.add(xt_Generator_Table);
		}
		Map<String, Object> condition = new HashMap<String, Object>();
		List<XtGeneratorForbidtable> list =  xtGeneratorForbidtableService.getXtGeneratorForbidtableListByCondition(condition);
		for(int i = 0; i < list.size(); i++){
			XtGeneratorForbidtable obj = list.get(i);
			for(int j = 0; j < xtGeneratorTableList.size(); j++){
				XtGeneratorTable xt_Generator_Table = xtGeneratorTableList.get(j);
				if(xt_Generator_Table.getTABLE_NAME().equals(obj.getXt_generator_f_table())){
					xtGeneratorTableList.remove(j);
					j--;
					break;
				}
			}
		}
		return outComboDataStr(xtGeneratorTableList);
	}

	
//	/**
//	 * 获取所有表Grid使用
//	 */
//	@NeedLoginUnAuth
//	@GetMapping(value="/table/grid/list")
//	@ApiOperation(value="获取所有表Grid使用", notes="获取所有表Grid使用")
//	@ResponseBody
//	public BaseResult getXtGeneratorTableGridList(HttpServletRequest request){
//		//支持多数据库如Mysql oracle  sqlserver sybase db2
//		List<XtGeneratorTable> xtGeneratorTableList = new ArrayList<XtGeneratorTable>();
//		DbInfo dbInfo = DBMSMetaUtil.excuteDB(null);
//		List<Map<String, Object>> tables = MapUtil.convertKeyList2LowerCase(dbInfo.getTables());
//		for(int i = 0; i < tables.size(); i++){
//			Map<String, Object> map = tables.get(i);
//			XtGeneratorTable xt_Generator_Table = new XtGeneratorTable();
//			if(null != map.get("remarks") && !"".equals(map.get("remarks"))){
//				xt_Generator_Table.setTABLE_COMMENT(""+map.get("remarks"));
//			}else{
//				xt_Generator_Table.setTABLE_COMMENT(""+map.get("table_name"));
//			}
//			xt_Generator_Table.setTABLE_NAME(""+map.get("table_name"));
//			xtGeneratorTableList.add(xt_Generator_Table);
//		}
//		Map<String, Object> condition = new HashMap<String, Object>();
//		List<XtGeneratorForbidtable> list =  xtGeneratorForbidtableService.getXtGeneratorForbidtableListByCondition(condition);
//		for(int i = 0; i < list.size(); i++){
//			XtGeneratorForbidtable obj = list.get(i);
//			for(int j = 0; j < xtGeneratorTableList.size(); j++){
//				XtGeneratorTable xt_Generator_Table = xtGeneratorTableList.get(j);
//				if(xt_Generator_Table.getTABLE_NAME().equals(obj.getXt_generator_f_table())){
//					xtGeneratorTableList.remove(j);
//					j--;
//					break;
//				}
//			}
//		}
//		return outItemsStr(xtGeneratorTableList);
//	}
//
	/**
	 * 获取可用硬盘
	 * @param request
	 */
	@NeedLoginUnAuth
	@GetMapping(value="/disks")
	@ApiOperation(value="可用硬盘", notes="可用硬盘")
	@ResponseBody
	public BaseResult getDisks(HttpServletRequest request){
		File[] arFileRoot = File.listRoots();
		JSONArray jsonArray = new JSONArray();  
		Map<String, Object> model = new HashMap<String, Object>();
		for(int i=0;i<arFileRoot.length;i++){
			model.put("disks", arFileRoot[i].getAbsolutePath().replaceAll(":\\\\","://"));
			model.put("disksName", arFileRoot[i].getPath().replaceAll(":\\\\","")+"盘");
			jsonArray.add(model);
		}
		return outComboDataStr(jsonArray);
	}


	/**
	 * 返回配置字段LIST
	 * @param xtGeneratorSearchFileJSON
	 * @return
	 */
	private List<XtGeneratorSearchFiled> commonGeneratorSearchFileJSONList(String xtGeneratorSearchFileJSON){
		JSONObject jsonObject = JSONObject.fromObject(xtGeneratorSearchFileJSON);
		Object object = jsonObject.get("searchFieldList");
		List<XtGeneratorSearchFiled> xtGeneratorSearchFileds = new ArrayList<XtGeneratorSearchFiled>();
		if(null != object){
			xtGeneratorSearchFileds = JsonUtil.toFastList(object,XtGeneratorSearchFiled.class);
			for(XtGeneratorSearchFiled xtGeneratorSearchFiled : xtGeneratorSearchFileds){
				if(!StringUtil.isEmpty(xtGeneratorSearchFiled.getXt_generator_search_flag())){
					if(xtGeneratorSearchFiled.getXt_generator_search_flag().equals("包含")){
						xtGeneratorSearchFiled.setXt_generator_search_flag("0");
					}else if(xtGeneratorSearchFiled.getXt_generator_search_flag().equals("等于")){
						xtGeneratorSearchFiled.setXt_generator_search_flag("1");
					}else if(xtGeneratorSearchFiled.getXt_generator_search_flag().equals("大于等于")){
						xtGeneratorSearchFiled.setXt_generator_search_flag("2");
					}else if(xtGeneratorSearchFiled.getXt_generator_search_flag().equals("小于等于")){
						xtGeneratorSearchFiled.setXt_generator_search_flag("3");
					}else if(xtGeneratorSearchFiled.getXt_generator_search_flag().equals("大于")){
						xtGeneratorSearchFiled.setXt_generator_search_flag("4");
					}else if(xtGeneratorSearchFiled.getXt_generator_search_flag().equals("小于")){
						xtGeneratorSearchFiled.setXt_generator_search_flag("5");
					}else if(xtGeneratorSearchFiled.getXt_generator_search_flag().equals("范围")){
						xtGeneratorSearchFiled.setXt_generator_search_flag("6");
					}
				}
				if(!StringUtil.isEmpty(xtGeneratorSearchFiled.getXt_generator_search_label_position())){
					if("居左".equals(xtGeneratorSearchFiled.getXt_generator_search_label_position().toString())){
						xtGeneratorSearchFiled.setXt_generator_search_label_position("0");
					}else{
						xtGeneratorSearchFiled.setXt_generator_search_label_position("1");
					}
				}
				if(!StringUtil.isEmpty(xtGeneratorSearchFiled.getXt_generator_search_type())){
					if(xtGeneratorSearchFiled.getXt_generator_search_type().toString().equals("文本框")){
						xtGeneratorSearchFiled.setXt_generator_search_type("0");
					}else if(xtGeneratorSearchFiled.getXt_generator_search_type().toString().equals("文本域")){
						xtGeneratorSearchFiled.setXt_generator_search_type("1");
					}else if(xtGeneratorSearchFiled.getXt_generator_search_type().toString().equals("数字框")){
						xtGeneratorSearchFiled.setXt_generator_search_type("2");
					}else if(xtGeneratorSearchFiled.getXt_generator_search_type().toString().equals("下拉框")){
						xtGeneratorSearchFiled.setXt_generator_search_type("3");
					}else if(xtGeneratorSearchFiled.getXt_generator_search_type().toString().equals("日期框")){
						xtGeneratorSearchFiled.setXt_generator_search_type("4");
					}else if(xtGeneratorSearchFiled.getXt_generator_search_type().toString().equals("文件框")){
						xtGeneratorSearchFiled.setXt_generator_search_type("5");
					}else{
						xtGeneratorSearchFiled.setXt_generator_search_type("0");
					}
				}
			}
		}
		return xtGeneratorSearchFileds;
	}
	
	/**
	 * 返回GridList集合
	 * @param xtGeneratorGridListJSON
	 * @return
	 */
	public List<XtGeneratorGridColumn> commonGeneratorGridListJSONList(String xtGeneratorGridListJSON){
		List<XtGeneratorGridColumn> xt_Generator_Grid_ColumnList = new ArrayList<XtGeneratorGridColumn>();
		JSONObject jsonObject = JSONObject.fromObject(xtGeneratorGridListJSON);
		Object object = jsonObject.get("column_name");
		if(null != object){
			List<String> list = (List<String>)object;
			List<String> columnCommentlist = (List<String>)jsonObject.get("column_comment");
			for(int i = 0; i < list.size(); i++){
				XtGeneratorGridColumn xt_Generator_Grid_Column = new XtGeneratorGridColumn();
				xt_Generator_Grid_Column.setXt_generator_grid_column_name(list.get(i));
				xt_Generator_Grid_Column.setXt_generator_grid_column_label(columnCommentlist.get(i));
				xt_Generator_Grid_ColumnList.add(xt_Generator_Grid_Column);
			}
		}
		return xt_Generator_Grid_ColumnList;
	}
	
	/**
	 * 返回新增 编辑 明细表单集合
	 * @param xtGeneratorTableColumnFormJson
	 * @return
	 */
	public List<XtGeneratorTableColumnForm> commonGeneratorFormJSONList(String xtGeneratorTableColumnFormJson){
		List<XtGeneratorTableColumnForm> xt_Generator_Table_Column_FormList = new ArrayList<XtGeneratorTableColumnForm>();
		JSONObject jsonObject = JSONObject.fromObject(xtGeneratorTableColumnFormJson);
		Object object = jsonObject.get("COLUMN_NAME");
		if(null != object){
			List<String> list = (List<String>)object;
			List<String> columnCommentlist = (List<String>)jsonObject.get("COLUMN_COMMENT");
			List<String> DATA_TYPElist = (List<String>)jsonObject.get("DATA_TYPE");
			List<String> CHARACTER_MAXIMUM_LENGTHlist = (List<String>)jsonObject.get("CHARACTER_MAXIMUM_LENGTH");
			List<String> IS_NULLABLElist = (List<String>)jsonObject.get("IS_NULLABLE");
			List<String> COLUMN_KEYlist = (List<String>)jsonObject.get("COLUMN_KEY");
			List<String> column_label_positionlist = (List<String>)jsonObject.get("column_label_position");
			List<String> column_label_anchorlist = (List<String>)jsonObject.get("column_label_anchor");
			List<String> column_typelist = (List<String>)jsonObject.get("column_type");
			List<String> isHiddenlist = (List<String>)jsonObject.get("isHidden");
			List<String> xt_script_idlist = (List<String>)jsonObject.get("xt_script_id");

			for(int i = 0; i < list.size(); i++){
				XtGeneratorTableColumnForm xt_Generator_Table_Column_Form = new XtGeneratorTableColumnForm();
				Object COLUMN_NAME = list.get(i);
				Object COLUMN_COMMENT = columnCommentlist.get(i);
				Object DATA_TYPE = DATA_TYPElist.get(i);
				Object CHARACTER_MAXIMUM_LENGTH = CHARACTER_MAXIMUM_LENGTHlist.get(i);
				Object IS_NULLABLE = IS_NULLABLElist.get(i);
				Object COLUMN_KEY = COLUMN_KEYlist.get(i);
				Object column_label_position = column_label_positionlist.get(i);
				Object column_label_anchor = column_label_anchorlist.get(i);
				Object column_type = column_typelist.get(i);
				Object isHidden = isHiddenlist.get(i);
				Object xt_script_id = xt_script_idlist.get(i);
				if(null != COLUMN_NAME && !"".equals(COLUMN_NAME)){
					xt_Generator_Table_Column_Form.setColumn_name((String)COLUMN_NAME);
				}
				if(null != COLUMN_COMMENT && !"".equals(COLUMN_COMMENT)){
					xt_Generator_Table_Column_Form.setColumn_comment((String)COLUMN_COMMENT);
				}
				if(null != DATA_TYPE && !"".equals(DATA_TYPE)){
					xt_Generator_Table_Column_Form.setData_type((String)DATA_TYPE);
				}
				if(null != CHARACTER_MAXIMUM_LENGTH && !"".equals(CHARACTER_MAXIMUM_LENGTH)){
					xt_Generator_Table_Column_Form.setCharacter_maximum_length((String)CHARACTER_MAXIMUM_LENGTH);
				}
				if(null != IS_NULLABLE && !"".equals(IS_NULLABLE)){
					xt_Generator_Table_Column_Form.setIs_nullable((String)IS_NULLABLE);
				}
				if(null != COLUMN_KEY && !"".equals(COLUMN_KEY)){
					xt_Generator_Table_Column_Form.setColumn_key((String)COLUMN_KEY);
				}
				if(null != column_label_position && !"".equals(column_label_position)){
					if("居左".equals(column_label_position.toString())){
						xt_Generator_Table_Column_Form.setColumn_label_position("0");
					}else{
						xt_Generator_Table_Column_Form.setColumn_label_position("1");
					}
				}
				if(null != column_label_anchor && !"".equals(column_label_anchor)){
					xt_Generator_Table_Column_Form.setColumn_label_anchor(""+column_label_anchor);
				}
				if(null != column_type && !"".equals(column_type)){
					if(column_type.toString().equals("文本框")){
						xt_Generator_Table_Column_Form.setColumn_type("0");
					}else if(column_type.toString().equals("文本域")){
						xt_Generator_Table_Column_Form.setColumn_type("1");
					}else if(column_type.toString().equals("数字框")){
						xt_Generator_Table_Column_Form.setColumn_type("2");
					}else if(column_type.toString().equals("下拉框")){
						xt_Generator_Table_Column_Form.setColumn_type("3");
					}else if(column_type.toString().equals("日期框")){
						xt_Generator_Table_Column_Form.setColumn_type("4");
					}else if(column_type.toString().equals("文件框")){
						xt_Generator_Table_Column_Form.setColumn_type("5");
					}else{
						xt_Generator_Table_Column_Form.setColumn_type("0");
					}
				}
				if(null != isHidden && !"".equals(isHidden)){
					if(isHidden.toString().equals("否")){
						xt_Generator_Table_Column_Form.setIsHidden("0");
					}else if(isHidden.toString().equals("是")){
						xt_Generator_Table_Column_Form.setIsHidden("1");
					}
				}
				if(null != xt_script_id && !"".equals(xt_script_id)){
					xt_Generator_Table_Column_Form.setXt_script_id((String)xt_script_id);
				}
				xt_Generator_Table_Column_FormList.add(xt_Generator_Table_Column_Form);
			}
		}
		return xt_Generator_Table_Column_FormList;
	}
	
	/**
	 * 删除
	 * @param xt_generator_id
	 */
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除", notes="删除")
	@ResponseBody
	public BaseResult delXtGenerator(String xt_generator_id){
		int i = 0;
		if(!StringUtils.isEmpty(xt_generator_id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("xt_generator_id",xt_generator_id.split(","));
			i=xtGeneratorService.delXtGenerator(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
	
	/**
	 * 返回一对多信息
	 * @param xtGeneratorOneToManyFormListJSON
	 * @return
	 */
	public List<XtGeneratorTableManyToOne> commonXtGeneratorOneToManyFormListJSON(String xtGeneratorOneToManyFormListJSON){
		List<XtGeneratorTableManyToOne> xtGeneratorTableManyToOnes = new ArrayList<XtGeneratorTableManyToOne>();
		if(!StringUtils.isEmpty(xtGeneratorOneToManyFormListJSON)){
			JSONObject jsonObject = JSONObject.fromObject(xtGeneratorOneToManyFormListJSON);
			Object object = jsonObject.get("oneToManyList");
			if(null != object){
				List<Map<String,Object>> list = (List)object;
				for(Map<String,Object> o: list){//Table
					XtGeneratorTableManyToOne xtGeneratorTableManyToOne = new XtGeneratorTableManyToOne();
					xtGeneratorTableManyToOne.setXt_generator_one_to_many_table_model_package(""+o.get("xt_generator_one_to_many_table_model_package"));
					xtGeneratorTableManyToOne.setXt_generator_one_to_many_table_dao_package(""+o.get("xt_generator_one_to_many_table_dao_package"));
					xtGeneratorTableManyToOne.setXt_generator_one_to_many_table_service_package(""+o.get("xt_generator_one_to_many_table_service_package"));
					xtGeneratorTableManyToOne.setXt_generator_one_to_many_table_web_package(""+o.get("xt_generator_one_to_many_table_web_package"));
					xtGeneratorTableManyToOne.setXt_generator_one_to_many_table_name_zh(""+o.get("xt_generator_one_to_many_table_name_zh"));
					xtGeneratorTableManyToOne.setXt_generator_one_to_many_table_fkey(""+o.get("xt_generator_one_to_many_table_fkey"));
					if(null != o.get("xt_generator_ontomany_grid")){
						String json = JsonUtil.toFastJson(o.get("xt_generator_ontomany_grid"));
						JSONObject jsonObj = JsonUtil.toJsonObj(json);
						Object dataObj  = jsonObj.get("xt_Generator_Table_ColumnMany_To_OneList");
//						json = JsonUtil.toFastJson(dataObj);
						List<XtGeneratorTableColumnManyToOne> xtGeneratorTableColumnManyToOneList = JsonUtil.toFastList(dataObj,XtGeneratorTableColumnManyToOne.class);
						xtGeneratorTableManyToOne.setXt_Generator_Table_ColumnMany_To_OneList(xtGeneratorTableColumnManyToOneList);
					}
//					xtGeneratorTableManyToOne.setXt_generator_ontomany_grid(o.get("xt_generator_ontomany_grid"));
					xtGeneratorTableManyToOne.setXt_generator_one_to_many_table_name(""+o.get("xt_generator_one_to_many_table_name"));
					xtGeneratorTableManyToOnes.add(xtGeneratorTableManyToOne);
				}
			}
			//一对多表中列信息
			for(XtGeneratorTableManyToOne xtGeneratorTableManyToOne: xtGeneratorTableManyToOnes){
				try {
					List<XtGeneratorTableColumnManyToOne> xtGeneratorTableColumnManyToOneList = xtGeneratorTableManyToOne.getXt_Generator_Table_ColumnMany_To_OneList();
					for(XtGeneratorTableColumnManyToOne xtGeneratorTableColumnManyToOne: xtGeneratorTableColumnManyToOneList){
						if(!StringUtil.isEmpty(xtGeneratorTableColumnManyToOne.getColumn_label_position())){
				    		if("居左".equals(xtGeneratorTableColumnManyToOne.getColumn_label_position())){
								xtGeneratorTableColumnManyToOne.setColumn_label_position("0");
				    		}else{
								xtGeneratorTableColumnManyToOne.setColumn_label_position("1");
				    		}
				    	}
				    	if(!StringUtil.isEmpty(xtGeneratorTableColumnManyToOne.getColumn_type())){
				    		if(xtGeneratorTableColumnManyToOne.getColumn_type().equals("文本框")){
								xtGeneratorTableColumnManyToOne.setColumn_type("0");
				    		}else if(xtGeneratorTableColumnManyToOne.getColumn_type().equals("文本域")){
								xtGeneratorTableColumnManyToOne.setColumn_type("1");
				    		}else if(xtGeneratorTableColumnManyToOne.getColumn_type().equals("数字框")){
								xtGeneratorTableColumnManyToOne.setColumn_type("2");
				    		}else if(xtGeneratorTableColumnManyToOne.getColumn_type().equals("下拉框")){
								xtGeneratorTableColumnManyToOne.setColumn_type("3");
				    		}else if(xtGeneratorTableColumnManyToOne.getColumn_type().equals("日期框")){
								xtGeneratorTableColumnManyToOne.setColumn_type("4");
				    		}else if(xtGeneratorTableColumnManyToOne.getColumn_type().equals("文件框")){
								xtGeneratorTableColumnManyToOne.setColumn_type("5");
				    		}else{
								xtGeneratorTableColumnManyToOne.setColumn_type("0");
				    		}
				    	}
				    	if(!StringUtil.isEmpty(xtGeneratorTableColumnManyToOne.getIsHidden())){
				    		if(xtGeneratorTableColumnManyToOne.getIsHidden().equals("否")){
								xtGeneratorTableColumnManyToOne.setIsHidden("0");
				    		}else if(xtGeneratorTableColumnManyToOne.getIsHidden().equals("是")){
								xtGeneratorTableColumnManyToOne.setIsHidden("1");
				    		}
				    	}
					}
					xtGeneratorTableManyToOne.setXt_Generator_Table_ColumnMany_To_OneList(xtGeneratorTableColumnManyToOneList);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return xtGeneratorTableManyToOnes;
		}
		return xtGeneratorTableManyToOnes;
	}

	/**
	 * 转换数据源配置
	 * @param xtDbinfo
	 * @return
	 */
	private DbInfo convert(XtDbinfo xtDbinfo){
		DbInfo dbInfo = new DbInfo();
		dbInfo.setIp(xtDbinfo.getXt_dbinfoIp());
		dbInfo.setPort(xtDbinfo.getXt_dbinfoPort());
		dbInfo.setUsername(xtDbinfo.getXt_dbinfoUName());
		dbInfo.setPassword(xtDbinfo.getXt_dbinfoPwd());
		dbInfo.setDatabasetype(xtDbinfo.getXt_dbinfoType());
		dbInfo.setDbname(xtDbinfo.getXt_dbinfoName());
		return dbInfo;
	}
}

